import configApi from "@/api/sys/config"
import dictApi from "@/api/admin/dict"

const sys = {
	state: {
		title : "小牛科技",
		version:"",
		login:"",
		company:"",
		initConfig:false,
		pageSize:20,//列表的默认页容量
		tableMaxHeight:420,//列表的默认页容量
		dict:[], //数据字典
		dict_status:false
	},
	getters: {
		getDict({state, commit}, param) {
			if(param.table){
				if(param.field){
					return  state[param.table][param.field];
				}
			}
		},
	},
	mutations: {
		SET_INITCONFIG( state, initConfig ) {
			state.initConfig = initConfig
		},
		SET_VERSION( state, version) {
			state.version = version
		},
		SET_LOGIN( state, login) {
			state.login = login
		},
		SET_TITLE( state, title) {
			state.title = title
		},
		SET_COMPANY( state, company) {
			state.company = company
		},
		SET_DICT(state , dict){
			state.dict = dict
			state.dict_status = true
		}
	},
	actions: {
		initSysConfig({ state,commit }, path) {
			return true;
			return new Promise(( resolve, reject )=>{
				if(!state.initConfig){
					configApi.getValues(["system_title","version","login","company_name"]).then(res=>{
						commit("SET_INITCONFIG",true)
						commit("SET_VERSION",res.data.version)
						commit("SET_LOGIN",res.data.login=="on"?true:false)
						commit("SET_TITLE",res.data.system_title)
						commit("SET_COMPANY",res.data.company_name)
						resolve()
					})
				}else{
					resolve()
				}
			})

		},
		getDict({ state,commit }, param) {
			return new Promise(( resolve, reject )=>{
				if(!state.dict_status){
					dictApi.gets(param).then(res=>{
						commit('SET_DICT' , res.data)
						resolve()
					})
				}else{
					resolve()
				}
			})
		}
	}
}
export default sys
